<script setup>
import { storeToRefs } from 'pinia'
import { useAppStore } from '../store'

const store = useAppStore()
const { isAltDown,input,info,loading,isReady,isCommand,options,inputRef,isNoDrag,addValue} = storeToRefs(store)
const { connect ,sendMessage,handleSelect,onInput,showCodePage,sendToAI} = store
window.codeAPI.sendToAI((value) => {
  sendToAI(value)
})
onMounted(() => {
  connect()

  function handleKeyDown(event) {
    if (event.key === 'Alt'  ) {
      isAltDown.value = false
    }
  }
  function handleKeyUp(event) {
    if ( event.key === 'Alt' ) {
      isAltDown.value = true
    }
  }

  window.addEventListener('keyup', handleKeyUp)
  window.addEventListener('keydown', handleKeyDown)
})



</script>

<template>
  <div id="container" class="container">
    <div class="bg">
        <div class="bg-left">
          <n-button class="setting" text @click="showCodePage()">
            <n-icon color="#000" size="22">
              <svg viewBox="0 0 512 512" xmlns="http://www.w3.org/2000/svg"><path d="M262.29 192.31a64 64 0 1 0 57.4 57.4a64.13 64.13 0 0 0-57.4-57.4zM416.39 256a154.34 154.34 0 0 1-1.53 20.79l45.21 35.46a10.81 10.81 0 0 1 2.45 13.75l-42.77 74a10.81 10.81 0 0 1-13.14 4.59l-44.9-18.08a16.11 16.11 0 0 0-15.17 1.75A164.48 164.48 0 0 1 325 400.8a15.94 15.94 0 0 0-8.82 12.14l-6.73 47.89a11.08 11.08 0 0 1-10.68 9.17h-85.54a11.11 11.11 0 0 1-10.69-8.87l-6.72-47.82a16.07 16.07 0 0 0-9-12.22a155.3 155.3 0 0 1-21.46-12.57a16 16 0 0 0-15.11-1.71l-44.89 18.07a10.81 10.81 0 0 1-13.14-4.58l-42.77-74a10.8 10.8 0 0 1 2.45-13.75l38.21-30a16.05 16.05 0 0 0 6-14.08c-.36-4.17-.58-8.33-.58-12.5s.21-8.27.58-12.35a16 16 0 0 0-6.07-13.94l-38.19-30A10.81 10.81 0 0 1 49.48 186l42.77-74a10.81 10.81 0 0 1 13.14-4.59l44.9 18.08a16.11 16.11 0 0 0 15.17-1.75A164.48 164.48 0 0 1 187 111.2a15.94 15.94 0 0 0 8.82-12.14l6.73-47.89A11.08 11.08 0 0 1 213.23 42h85.54a11.11 11.11 0 0 1 10.69 8.87l6.72 47.82a16.07 16.07 0 0 0 9 12.22a155.3 155.3 0 0 1 21.46 12.57a16 16 0 0 0 15.11 1.71l44.89-18.07a10.81 10.81 0 0 1 13.14 4.58l42.77 74a10.8 10.8 0 0 1-2.45 13.75l-38.21 30a16.05 16.05 0 0 0-6.05 14.08c.33 4.14.55 8.3.55 12.47z" fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="32"></path></svg>
            </n-icon>
          </n-button>
        </div>
        <div class="bg-middle">
          <n-dropdown
              :options="options"
              :show="isCommand"
              placement="bottom-start"
              size="small"
              style="max-height: 400px;overflow: auto"
              @select="handleSelect">
            <input ref="inputRef" v-model="input" class="user-input" @input="onInput"  @keydown.enter="sendMessage" />
          </n-dropdown>
<!--          <n-select-->
<!--              class="user-input"-->
<!--              :options="options"-->
<!--              :filterable="true"-->
<!--              :consistent-menu-width="true"-->
<!--          />-->
<!--          <n-auto-complete-->
<!--              class="user-input"-->
<!--              v-model="input"-->
<!--              :options="options"-->
<!--              placeholder="按下 “/” 调出指令列表"-->
<!--              :get-show="getShow"-->
<!--          />-->
<!--          <n-mention class="user-input" -->
<!--                     :options="options"-->
<!--                     :prefix="['/', '、']"-->
<!--                     @search="sendMessage"-->
<!--                     @input="onInput"-->
<!--                     v-model:value="input"/>-->
        </div>
      <div class="bg-right">
        <template v-if="isAltDown">
          <n-popselect v-model:value="addValue" :options="options" trigger="click" >
            <n-button class="add" color="#FFF" size="tiny" >
              <n-icon color="#000" size="22">
                <svg viewBox="0 0 512 512" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><path d="M256 112v288" fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="32"></path><path d="M400 256H112" fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="32"></path></svg>
              </n-icon>
            </n-button>
          </n-popselect>
        </template>
        <template v-else>
          <n-button class="add" color="#FFF" size="tiny" >
            <n-icon color="#000" size="22">
              <svg viewBox="0 0 512 512" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><path d="M160 368L32 256l128-112" fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="32"></path><path d="M352 368l128-112l-128-112" fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="32"></path><path d="M304 96l-96 320" fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="32"></path></svg>
            </n-icon>
          </n-button>
        </template>
      </div>
    </div>


    <div :class="{'no-drag':isNoDrag}" class="info">
      <div class="info-left">
        <div :class="[isReady ? 'green' : 'red' , {'loading' : loading}]"></div>
      </div>
      <div class="info-min">
        {{ info }}
      </div>

    </div>

  </div>



</template>

<style scoped>
@keyframes bounce {
  0% { transform: translateX(00px) scale(0.7); }
  25% { transform: translateX(10px) scale(1); }
  50% { transform: translateX(0px) scale(0.7); }
  75% { transform: translateX(-10px) scale(1); }
  100% { transform: translateX(0px) scale(0.7); }
}

.red{
  width: 8px;
  height: 8px;
  background-color: rgba(255, 60, 0, 0.78);
  border-radius: 50%;
}

.green{
  width: 8px;
  height: 8px;
  background-color: green;
  border-radius: 50%;
  align-items: center;
}

.loading{
  width: 10px;
  height: 10px;
  border-radius: 50%;
  animation: bounce 1.5s infinite;
  animation-timing-function: cubic-bezier(.69,.13,.3,.86)
}

.setting{
  margin: auto;
  -webkit-app-region: no-drag;
}

.user-input{
  height: 30px;
  padding-left: 10px;
  padding-right: 10px;
  flex: 1;
  border: none;
  border-radius: 5px;
  -webkit-app-region: no-drag;
}

.user-input:focus{
  outline: none;
}

.container{
  background-color: #F6F8FA;
  border-radius: 5px;
  padding: 5px 5px 0 5px;
  margin: 5px;
  box-shadow:#555555 1px 1px 3px;
  overflow: hidden;
  box-sizing: border-box;
}

.bg{
  display: grid;
  grid-template-columns: 30px 1fr 40px;
  grid-template-rows: 1fr;
  gap: 0 5px;
  grid-auto-flow: row;
  grid-template-areas:"bg-left bg-middle bg-right";
  background-color: #C5CEDB;
  border-radius: 5px;
  padding: 5px;
  box-sizing: border-box;
}


.bg-left{
  grid-area: bg-left;
  display:flex;
  align-items:center;
}
.bg-middle{
  grid-area: bg-middle;
  display:flex;
  align-items:center;
}
.bg-right{
  grid-area: bg-right;
  display:flex;
  align-items:center;
}

.add{
  width: 40px;
  height: 32px;
  -webkit-app-region: no-drag;
}

.info{
  display: grid;
  grid-template-columns: 20px 1fr 42px;
  grid-template-rows: 30px 1fr;
  gap: 0 0;
  grid-auto-flow: row;
  grid-template-areas:"info-left info-min info-right"
"info-bottom info-bottom info-bottom";
  padding-left: 10px;
  align-items:center;
  box-sizing: border-box;
  overflow: hidden;
  max-height: 400px;
}

.info-left{
  grid-area: info-left;
  display:flex;
  justify-content: center;
}

.info-min{
  grid-area: info-min;
  display:flex;
  padding-left: 9px;
  font-size: 13px;
}

.info-right{
  grid-area: info-right;
  display:flex;
  justify-content: flex-end;
}
.info-right :hover{
  grid-area: info-right;
  display:flex;
  justify-content: flex-end;
  color: #d000ff;
}



</style>